package icom.icss.controller;

import icom.icss.entity.PageBean;
import icom.icss.service.UserService;
import icom.icss.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "PageServlet",urlPatterns = "/page")
public class PageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        //当前页
        String current_Page = request.getParameter("currentPage");
        //每页显示多少条数据
        String count_ = request.getParameter("count");
        int currentPage = 1;
        if(current_Page!=null){
            currentPage = Integer.parseInt(current_Page);
        }
        int count = 5;//默认每页显示4条
        if(count_ != null){
            count = Integer.parseInt(count_);
        }
        //调用业务逻辑
        UserService userService = new UserServiceImpl();
        PageBean pageBean = null;
        try {
            pageBean = userService.selectPageBean(currentPage,count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //存入request中
        request.setAttribute("pageBean" , pageBean);
        //转发
        request.getRequestDispatcher("user-list.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
